<template>
  <div class="mock-test-page">
    <div class="page-header">
      <h1>模拟API测试</h1>
      <p>测试后端模拟API接口</p>
    </div>
    
    <div class="test-container">
      <!-- 天气API测试 -->
      <div class="test-section">
        <h2>天气API测试</h2>
        <button @click="testWeather" :disabled="loading" class="test-btn">
          {{ loading ? '测试中...' : '测试天气API' }}
        </button>
        <div v-if="weatherResult" class="result">
          <h3>结果:</h3>
          <pre>{{ JSON.stringify(weatherResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- 地理编码API测试 -->
      <div class="test-section">
        <h2>地理编码API测试</h2>
        <button @click="testGeocode" :disabled="loading" class="test-btn">
          {{ loading ? '测试中...' : '测试地理编码API' }}
        </button>
        <div v-if="geocodeResult" class="result">
          <h3>结果:</h3>
          <pre>{{ JSON.stringify(geocodeResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- POI搜索API测试 -->
      <div class="test-section">
        <h2>POI搜索API测试</h2>
        <button @click="testPoi" :disabled="loading" class="test-btn">
          {{ loading ? '测试中...' : '测试POI搜索API' }}
        </button>
        <div v-if="poiResult" class="result">
          <h3>结果:</h3>
          <pre>{{ JSON.stringify(poiResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- 路径规划API测试 -->
      <div class="test-section">
        <h2>路径规划API测试</h2>
        <button @click="testRoute" :disabled="loading" class="test-btn">
          {{ loading ? '测试中...' : '测试路径规划API' }}
        </button>
        <div v-if="routeResult" class="result">
          <h3>结果:</h3>
          <pre>{{ JSON.stringify(routeResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- 错误信息 -->
      <div v-if="error" class="error">
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';

// 使用API composable
const { 
  getWeatherMock, getGeocodeMock, getPoiMock, getRouteMock 
} = useApi();

// 响应式数据
const loading = ref(false);
const error = ref('');
const weatherResult = ref<any>(null);
const geocodeResult = ref<any>(null);
const poiResult = ref<any>(null);
const routeResult = ref<any>(null);

// 测试天气API
const testWeather = async () => {
  loading.value = true;
  error.value = '';
  
  try {
    const result = await getWeatherMock({ city: '广州' });
    weatherResult.value = result;
  } catch (err) {
    console.error('天气API测试失败:', err);
    error.value = '天气API测试失败';
  } finally {
    loading.value = false;
  }
};

// 测试地理编码API
const testGeocode = async () => {
  loading.value = true;
  error.value = '';
  
  try {
    const result = await getGeocodeMock({ 
      location: '113.289405,23.013125',
      type: 'regeo'
    });
    geocodeResult.value = result;
  } catch (err) {
    console.error('地理编码API测试失败:', err);
    error.value = '地理编码API测试失败';
  } finally {
    loading.value = false;
  }
};

// 测试POI搜索API
const testPoi = async () => {
  loading.value = true;
  error.value = '';
  
  try {
    const result = await getPoiMock({ 
      location: '113.289405,23.013125',
      keywords: '餐厅'
    });
    poiResult.value = result;
  } catch (err) {
    console.error('POI搜索API测试失败:', err);
    error.value = 'POI搜索API测试失败';
  } finally {
    loading.value = false;
  }
};

// 测试路径规划API
const testRoute = async () => {
  loading.value = true;
  error.value = '';
  
  try {
    const result = await getRouteMock({ 
      origin: '113.289405,23.013125',
      destination: '113.329405,23.005125'
    });
    routeResult.value = result;
  } catch (err) {
    console.error('路径规划API测试失败:', err);
    error.value = '路径规划API测试失败';
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.mock-test-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.page-header {
  text-align: center;
  color: white;
  margin-bottom: 30px;
}

.page-header h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  font-weight: 300;
}

.page-header p {
  font-size: 1.1rem;
  opacity: 0.9;
}

.test-container {
  max-width: 800px;
  margin: 0 auto;
  display: grid;
  gap: 20px;
}

.test-section {
  background: white;
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.test-section h2 {
  color: #333;
  margin-bottom: 20px;
  font-size: 1.5rem;
}

.test-btn {
  padding: 12px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.test-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}

.test-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.result {
  margin-top: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  border-left: 4px solid #667eea;
}

.result h3 {
  color: #333;
  margin-bottom: 15px;
  font-size: 1.2rem;
}

.result pre {
  background: #2d3748;
  color: #e2e8f0;
  padding: 15px;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 12px;
  line-height: 1.4;
}

.error {
  background: #fff5f5;
  color: #e53e3e;
  padding: 15px;
  border-radius: 8px;
  border: 1px solid #fed7d7;
  text-align: center;
  font-size: 14px;
}
</style> 